﻿
using System.IO;

namespace LS.Editor.DataTableTools
{
    public sealed partial class DataTableProcessor
    {
        private sealed class ByteProcessor : GenericDataProcessor<byte>
        {
            public override bool IsSystem
            {
                get
                {
                    return true;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "byte";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "byte",
                    "system.byte"
                };
            }

            public override byte Parse(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return byte.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                binaryWriter.Write(Parse(value));
            }
        }
        private sealed class ByteArrayProcessor : GenericDataArrayProcessor<byte>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "byte[]";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "byte[]",
                    "system.byte[]",
                };
            }

            public override byte ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return byte.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    binaryWriter.Write(array[i]);
                }
            }
        }
        private sealed class ByteListProcessor : GenericDataListProcessor<byte>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "List<bool>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "list<byte>",
                    "list<system.byte>",
                    "system.collections.generic.list<byte>",
                    "system.collections.generic.list<system.byte>",
                };
            }

            public override byte ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return byte.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Count);
                for (int i = 0; i < array.Count; i++)
                {
                    binaryWriter.Write(array[i]);
                }
            }
        }
        private sealed class ByteDictionaryProcessor : GenericDataDictionaryProcessor<byte>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Dictionary<string,bool>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "dictionary<string,byte>",
                    "dictionary<string,system.byte>",
                    "dictionary<system.string,byte>",
                    "dictionary<system.string,system.byte>",
                    "system.collections.generic.dictionary<string,byte>",
                    "system.collections.generic.dictionary<string,system.byte>",
                    "system.collections.generic.dictionary<system.string,byte>",
                    "system.collections.generic.dictionary<system.string,system.byte>",
                };
            }

            public override byte ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return byte.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var dic = Parse(value);
                binaryWriter.Write(dic.Count);
                foreach (var item in dic)
                {
                    binaryWriter.Write(item.Key);
                    binaryWriter.Write(item.Value);
                }
            }
        }
    }
}
